package com.ruiyi.subway.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import com.ruiyi.subway.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;


public abstract class SupperController {

	protected Logger log = LoggerFactory.getLogger(getClass());
	protected HashMap<String,Object> map=new HashMap<>();


	/**
	 * 提交后跳转页面
	 * @param flushFlag 是否刷新父页面
	 */
	/*protected String submitSuccess(HttpServletRequest request,boolean flushFlag) {
		request.setAttribute("flushFlag", flushFlag);
		return "forward:" + "/common/submitSuccess.jsp";
	}
	*/
	

	/**
	 * 输出字符串
	 * 
	 * @param str
	 */
	protected void outString(String str, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 输出json字符串 
	 * 
	 * @param str
	 */
	protected void outJson(String json, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 输出json Boolean
	 * 
	 * @param flag true或false
	 */
	protected void outJsonForRs(Boolean flag, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{\"rs\":" + flag + "}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 输出json Boolean
	 * 
	 * @param flag true或false
	 * @param msg 提示内容
	 */
	protected void outJsonForRs(Boolean flag, String msg,HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{rs:" + flag + ",msg:\'"+msg+"'}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 输出json字符串
	 * 
	 * @param flag
	 */
	protected void outJsonForRs(String rs, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{\"rs\":" + rs + "}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 *输出json Boolean
	 * 
	 * @param flag
	 */
	protected void outJsonForSuccessMsg(Boolean flag, String msg,HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{\"success\":" + flag + ",msg:\'"+msg+"'}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	/**
	 *输出json Boolean
	 * 
	 * @param flag
	 */
	protected void outJsonForSuccess(Boolean flag, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{\"success\":" + flag + "}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	/**
	 *输出json Boolean
	 * 
	 * @param flag
	 */
	protected void outJsonForSuccessParam(Boolean flag,String param, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.write("{\"success\":" + flag +","+param+ "}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 将对象转换成json输出
	 * 
	 * @param obj
	 * @throws Exception
	 */
	protected void outJsonForObject(Object obj, HttpServletResponse response)
			throws Exception {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			String json = JSONUtils.bean2json(obj);
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 将对象转换成json输出
	 * 
	 * @param obj
	 * @throws Exception
	 */
	protected void outJsonForMap(Map map, HttpServletResponse response)
			throws Exception {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			String json = JSONUtils.map2json(map);
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	
	/**
	 * 将对象转换成json输出
	 * 
	 * @param obj
	 * @throws Exception
	 */
	protected void outJsonList(List list, HttpServletResponse response)
			throws Exception {
		PrintWriter out = null;
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			String json = JSONUtils.list2json(list);
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	/**
	 * 输出加载的json对象
	 * @param map
	 * @throws Exception 
	 */
	protected void outLoadJson(Map map,HttpServletResponse response) throws Exception{
		String json= JSONUtils.map2json(map);
		this.outJsonForSuccessParam(true, "data:" + json, response);
	}

	// --------request Map类型转换------------------------------------
	protected String parseString(Map paramMap, String key) {
		return paramMap.get(key).toString();
	}

	/**
	 * 将paramMap中的key转换成Long型
	 * @param paramMap
	 * @param key
	 * @return
	 */
	protected Long parseLong(Map paramMap, String key) {
		return Long.parseLong(paramMap.get(key).toString());
	}
}
